package com.cj.controller;
import com.cj.po.DClass;
import com.cj.po.Dormitory;
import com.cj.po.PageInfo;
import com.cj.service.DormitoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户控制器类
 */
@Controller
public class DormitoryContoller {
    // 依赖注入
    @Autowired
    private DormitoryService dormitoryService; // 修改为小写 dormitoryStudentService

    @RequestMapping(value = "/findDormitory")
    public String findDormitory(Integer s_dormitoryid, String d_dormbuilding, String d_bedtotal,String d_bed, String a_name, Integer pageIndex, Integer pageSize, Model model) {

//        // 获取分页信息
        PageInfo<Dormitory> pi = dormitoryService.findPageInfo( s_dormitoryid,  d_dormbuilding,  d_bedtotal, d_bed, a_name,
                pageIndex, pageSize);
        model.addAttribute("di", pi);
        model.addAttribute("s_dormitoryid", s_dormitoryid);
        return "dormitory_list"; // 返回视图
    }

    /**
     * 导出Excel
     */
    @RequestMapping(value = "/exportdormitorylist", method = RequestMethod.POST)
    @ResponseBody
    public List<Dormitory> exportDormitory() {
        // 获取所有班级信息
        List<Dormitory> dormitoryList = dormitoryService.getAll();
        return dormitoryList;
    }



    /**
     * 添加宿舍信息
     */
    @RequestMapping(value = "/addDormitory", method = RequestMethod.POST)
    @ResponseBody
    public String addDormitory(@RequestBody Dormitory dormitory) { // 修改为 dormitory
        int result = dormitoryService.addDormitory(dormitory); // 添加宿舍
        return "dormitory_list"; // 返回宿舍列表视图
    }

    /**
     * 删除宿舍信息
     */
    @RequestMapping("/deleteDormitory")
    @ResponseBody
    public String deleteDormitory(Integer d_id) { // 修改参数为 d_id
        int result = dormitoryService.deleteDormitory(d_id); // 删除宿舍
        return "dormitory_list"; // 返回宿舍列表视图
    }

    /**
     * 修改宿舍信息
     */
    @RequestMapping(value = "/updateDormitory", method = RequestMethod.POST)
    public String updateDormitory(Dormitory dormitory) { // 修改为 DClass
        int result = dormitoryService.updateDormitory(dormitory); // 更新班级信息
        return "redirect:/findDormitory"; // 跳转到班级列表
    }

    /**
     * 根据宿舍ID查找宿舍信息
     */
    @RequestMapping("/findDormitoryById")
//    @ResponseBody
    public String findDormitoryById(Integer d_id, HttpSession session) { // 修改为 c_id
        Dormitory dormitory = dormitoryService.findDormitoryById(d_id); // 查找班级信息
        session.setAttribute("d", dormitory); // 将班级信息存入会话
        return "dormitory_edit"; // 返回班级编辑页面
    }
}
